<?php

class Frontpage_m extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->load->database();
    }
    
    //FRONTPAGE
    public function get_frontpage($page=0,$id=null){
        $offset = 20;
        $start = $page*$offset;
        $this->db->limit($offset,$start);
        $this->db->order_by('modified_date','desc');
        if(!empty($id)){
            $this->db->where('id',$id);
        }
        return $this->db->get('frontpage');
    }
    
    public function get_home_frontpage($page=0,$id=null){
        $offset = 4;
        $start = $page*$offset;
        $this->db->limit($offset,$start);
        $this->db->order_by('modified_date','desc');
        if(!empty($id)){
            $this->db->where('id',$id);
        }
        return $this->db->get('frontpage');
    }
    
    public function get_frontpage_count(){
        return $this->db->count_all_results('frontpage');
    }
    
    public function add($input){
        //another field
        $this->db->set('name',$input['name']);
        $this->db->set('type',$input['type'],FALSE);
        $this->db->set('img1',$input['img1']);
        $this->db->set('link1',$input['link1']);
        $this->db->set('text1',$input['text1']);
        if($input['type']>=1){
            $this->db->set('img2',$input['img2']);
            $this->db->set('link2',$input['link2']);
            $this->db->set('text2',$input['text2']);
        }
        if($input['type']==2){
            $this->db->set('img3',$input['img3']);
            $this->db->set('link3',$input['link3']);
            $this->db->set('text3',$input['text3']);
        }
        $this->db->set('modified_date','NOW()',FALSE);
        $this->db->insert('frontpage');
    }
    
    public function edit($input){
        //main image
        if(!empty($input['img1'])){
            $this->delete_photo($input['id'],0);
            $this->db->set('img1',$input['img1']);
        }
        if(!empty($input['img2'])){
            $this->delete_photo($input['id'],1);
            $this->db->set('img2',$input['img2']);
        }
        if(!empty($input['img3'])){
            $this->delete_photo($input['id'],2);
            $this->db->set('img3',$input['img3']);
        }
        //another field
        $this->db->set('name',$input['name']);
        $this->db->set('type',$input['type'],FALSE);
        $this->db->set('link1',$input['link1']);
        $this->db->set('text1',$input['text1']);
        $this->db->set('link2',$input['link2']);
        $this->db->set('text2',$input['text2']);
        $this->db->set('link3',$input['link3']);
        $this->db->set('text3',$input['text3']);
        $this->db->where('id',$input['id']);
        $this->db->update('frontpage');
    }
    
    public function delete($id){
        $this->delete_photo($id,0);
        $this->delete_photo($id,1);
        $this->delete_photo($id,2);
        $this->db->where('id',$id,FALSE);
        $this->db->delete('frontpage');
    }
    
    // PHOTO
    public function delete_photo($id,$row=0){
        $this->db->where('id',$id);
        $result = $this->db->get('frontpage');
        $link = '';
        if($row==0){
            $link = $result->first_row()->img1;
        } else if($row==1){
            $link = $result->first_row()->img2;
        } else{
            $link = $result->first_row()->img3;
        }
        $path ='./photo/frontpage/'.$link;
        if(is_file($path)){
            unlink($path);
        }
    }
}